<?php
/**
 * User: @lose
 * Date: 2019-10-10
 * Time: 18:02
 */

namespace Biz\service\admin;

use Biz\model\store\StoreAccount as mAccount;
use Biz\model\store\StoreAuth as mAuth;
use Biz\model\store\StoreOperator as mOperator;
use Biz\model\store\StoreBasic as mBasic;

class StoreAccount
{
    public static function updateAccount($id , $data)
    {
       return  mAccount::updateById($id  , $data);
    }


    public static function getList($where, $limit)
    {
        $count = mAccount::getCount($where);
        $where['LIMIT'] = $limit;
        $where['ORDER'] = ['addtime' => 'DESC'];
        $list  =  mAccount::getListByWhere($where );
        if($list) {
           foreach ($list as &$val){
               $val['complete_name'] ='填写完整';
               $val['complete_status'] = 0;
               $operate_info =  mOperator::getInfoByAid($val['id']);
               $auth_info = mAuth::getInfoByAid($val['id']);
               if(empty($operate_info) || empty($auth_info)){
                    $val['complete_status'] =1;
                   $val['complete_name'] ='不完整';

               }


               $val['server_type'] = mAccount::SERVER_TYPE_MAP[$val['server_type']];
               $val['auth_status_name'] = mAccount::AUTH_MAP[$val['auth_status']];
           }
        }
        return ['list' => $list, 'total' =>$count];
    }



    public static function updateOperator($data , $id)
    {
        return  mOperator::updateById($id , $data);
    }

    public static function InsertOperator($data)
    {

        return mOperator::add($data);
    }


    public static function getOperatorByAId($account_id)
    {
        $operator_info = [];
        if($account_id){
             $operator_info = mOperator::getInfoByAid($account_id);
        }
        return $operator_info;
    }


    public static function updateAuth($data , $id)
    {
        return  mAuth::updateById($id , $data);
    }

    public static function InsertAuth($data)
    {
        return mAuth::add($data);
    }


    public static function getAuthByAId($account_id)
    {
        $auth_info = [];
        if($account_id){
            $auth_info = mAuth::getInfoByAid($account_id);
        }
        return $auth_info;
    }



    /*生成店铺id
     *
     * */
    public static function createStoreBasic($account_id , $uid ,$name , $store_category = '' )
    {
        $basic_info = mBasic::getInfoByAid($account_id);
        if($basic_info && is_array($basic_info)){
            $data = [];
            $data['uid'] = $uid;
            $data['account_id'] = $account_id;
            $data['name'] = $name;
            $data['store_category'] = $store_category;
            $rs =  mBasic::updateById($basic_info['id'],$data);
            //修改store_category
            \Biz\service\Store\StoreHomeService::updateStoreCategoryForCase($basic_info['id'] ,$store_category);
            mAccount::updateById($account_id ,['store_id' => $basic_info['id']]);
        }else{
            $data = [];
            $data['uid'] = $uid;
            $data['account_id'] = $account_id;
            $data['name'] = $name;
            $data['store_category'] = $store_category;
            $rs =  mBasic::add($data);
            mAccount::updateById($account_id ,['store_id' => $rs]);
        }
        return $rs;
    }

    /*修改店铺基础信息
     *
     * */
    public static function updateStoreBasic($store_id , $data)
    {
       mBasic::updateById($store_id , $data);
    }
}